package com.edgar.baselibrary.utils;

import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

/**
 * Created by edgar on 2016/1/18.
 */
public class SpannedUtils {

    private static final String FONT_HTML = "%s<font color=%s>%s</font>";
    public static SpannableString convertSpannedString(String str){
        return new SpannableString(str);
    }

    /**
     * str转spanned
     * @param str
     * @param color
     * @param start
     * @param end
     * @return
     */
    public static Spannable convertSpanned(String str,int color,int start,int end){
        SpannableString spannableString = convertSpannedString(str);
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(color);
        spannableString.setSpan(foregroundColorSpan,start,end,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    /**
     * 转换html spanned
     * @param str
     * @param fontStr
     * @param color
     * @return
     */
    public static Spanned convertHtmlSpanned(String str,String fontStr,String color){
        if(TextUtils.isEmpty(str)) str = "";
        if(TextUtils.isEmpty(color)){
            throw new IllegalArgumentException("color is null.");
        }
        return Html.fromHtml(String.format(FONT_HTML,str,color,fontStr));
    }
}